package com.twotiger.and.activity.project;

import java.util.HashMap;

import android.content.Intent;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import app.twotiger.p2p.R;

import com.alibaba.fastjson.JSONObject;
import com.twotiger.and.Constants;
import com.twotiger.and.activity.HomeActivity;
import com.twotiger.and.activity.base.BaseActivity;
import com.twotiger.and.activity.base.BaseHandler;
import com.twotiger.and.activity.base.SubTitle;
import com.twotiger.and.bean.Basebean;
import com.twotiger.and.bean.Share;
import com.twotiger.and.util.UmShearUtils;

/**
 * 
 * @Description: 
 * 直投成功、转让成功页面
 * @Author douxy
 * @Date 2015年8月12日 下午1:34:44
 */
public class InvestSuccessPage extends BaseActivity {
    

    public static final String TYPE_TENDER = "investtender";//业务流程---直投项目
    public static final String TYPE_DEBT = "investdebt";//业务流程---转让项目投资
    

    private static final int GETSHARESUCCESS = 0;
    private static final int GETSHAREFAILURE = 1;


    private static HashMap<String, String> params;
    private SubTitle subTitle;
    private Button btn_share;
    private TextView tv_msg;
    private TextView tv_assert;
    private String title;
    private String type;


    @Override
    protected void processLogic() {
        params = getParm();

        title = getIntent().getStringExtra("TITLE");
        type = getIntent().getStringExtra("TYPE");
        
        initPage();

    }

    private void initPage() {
        subTitle.setTitle(title);
        tv_msg.setText(title+"");
    }

    @Override
    protected void setListener() {
        
        btn_share.setOnClickListener(this);
        tv_assert.setOnClickListener(this);

        handler = new BaseHandler(this) {

            @Override
            public void handleMessage(Message msg) {
                switch (msg.what) {
                case GETSHARESUCCESS:
                    Basebean result = JSONObject.parseObject((String) msg.obj, Basebean.class);
                    if(result.isOk()){
                        Share shareInfo = JSONObject.parseObject(result.data, Share.class);
                        String title = shareInfo.getTitle();
                        String content = shareInfo.getContent();
                        String url = shareInfo.getUrl();
                        String pic = shareInfo.getPic();
                        UmShearUtils.shear(InvestSuccessPage.this, url, true, title, content,pic);
                    }else{
                        showToast(result.codeDesc);
                    }
                    break;
                case GETSHAREFAILURE:
                    showToast("网络异常");
                    break;
                default:
                    break;
                }

                super.handleMessage(msg);
            }
        };

    }



    @Override
    protected void findViewById(View view) {
        subTitle = new SubTitle(view) {
            @Override
            protected void clickTitle(View v) {
            }

            @Override
            protected void clickRightBtn(View v) {
            }

            @Override
            protected void cickLeftBtn(View v) {
                onBackPress();
            }
        };
        subTitle.setTitle("总资产");
        subTitle.hideBtns();
        subTitle.title_left_bt.setVisibility(View.VISIBLE);

        tv_msg = (TextView) view.findViewById(R.id.tv_msg);
        btn_share = (Button) view.findViewById(R.id.btn_share);
        tv_assert = (TextView) view.findViewById(R.id.tv_assert);

    }

    @Override
    protected View loadViewLayout(LayoutInflater inflater) {
        View view = inflater.inflate(R.layout.invest_success_layout, null);
        return view;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
        case R.id.btn_share:
            gotoShare();
            break;
        case R.id.tv_assert:
            startNewActivity(new Intent(InvestSuccessPage.this, HomeActivity.class).putExtra(
                "BUTTON", R.id.radio_btn4), R.anim.push_left_in, R.anim.push_left_out, true);
            break;
        default:
            break;

        }
    }
    
    /**
     * 分享
     */
    protected void gotoShare() {
        params = getParm();
        params.clear();
        if(TYPE_TENDER.equals(type)){
            params.put("type", "inProject");
        }else if(TYPE_DEBT.equals(type)){
            params.put("type", "inBondProject");
        }
        params.put("token", getToken());
        getDate(params, Constants.SHARE, handler, GETSHARESUCCESS, GETSHAREFAILURE, true, false, false);
    }
    
    @Override
    protected void onBackPress() {
        setResult(ProjectDetailPage.TENDERRESULTCLOSE_SUC);
        finish();
        overridePendingTransition(R.anim.push_right_in, R.anim.push_right_out);
    }


}
